﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Prototype_C_Sharp
{
    public interface IPrototype : ICloneable
    {
        object DeepClone();
    }

    [Serializable]
    public class Prototype
        : IPrototype
    {
        public object Clone() => MemberwiseClone();

        public object DeepClone()
        {
            //创建内存流
            using (var ms = new MemoryStream())
            {
                //创建序列化器
                BinaryFormatter formater = new BinaryFormatter();
                //序列化
                formater.Serialize(ms, this);
                ms.Seek(0, SeekOrigin.Begin);
                //读取流
                return formater.Deserialize(ms);
            }
        }
    }
}
